﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.Class;
using WindowsForms.service;
using System.IO;
using System.Drawing.Imaging;

namespace WindowsForms
{
    public partial class MenuProcess : DevExpress.XtraEditors.XtraForm
    {
        private string picture;
        Service1 iQuery;
        private List<Units> lsUnit;
        private List<MenuTypes> lsMenuType;

        public MenuProcess(bool? isModifyer = null)
        {
            iQuery = new Service1();
            InitializeComponent();
            GetUnits();
            GetMenuTypes();
            if (isModifyer.HasValue)
            {
                GenerateModifyerControl(isModifyer.Value);
            }
        }

        private void GetUnits()
        {
            ConvertToList convert = new ConvertToList();

            lsUnit = convert.ConvertTolistUnits(iQuery.SelectUnitsAll().Tables[0]);
            cbUnit.Properties.DataSource = lsUnit;
        }

        private void GetMenuTypes()
        {
            ConvertToList convert = new ConvertToList();
            lsMenuType = convert.ConvertTolistMenuTypes(iQuery.SelectMenuTypesAll().Tables[0]);
            cbMenuType.Properties.DataSource = lsMenuType;
        }

        private void GenerateModifyerControl(bool isModifyer)
        {
            if (isModifyer)
            {
                this.layoutControlGroupMenu.Text = "Chỉnh sửa thông tin món";
                this.layoutControlGroupMenu.CustomizationFormText = "Chỉnh sửa thông tin món";
                this.layoutControlGroupMenu.CaptionImage = global::WindowsForms.Properties.Resources.Edit;
            }
        }
        #region get, set
        public string Menu_Price
        {
            get
            {
                return spinEditPrice.Text;
            }
            set
            {
                spinEditPrice.Text = value;
            }
        }

        public string Menu_Total
        {
            get
            {
                return spinEditTotal.Text;
            }
            set
            {
                spinEditTotal.Text = value;
            }
        }

        public int Menu_MenuTypeID
        {
            get
            {
                return (int)cbMenuType.EditValue;
            }
            set
            {
                cbMenuType.EditValue = value;
            }
        }
        public MenuTypes MenuType_Selected
        {
            get
            {
                return lsMenuType.Find(delegate(MenuTypes id)
                {
                    return id.MenuTypeID == (int)cbMenuType.EditValue;
                });
            }
        }

        public string Menu_Name
        {
            get
            {
                return txtMenuName.Text;
            }
            set
            {
                txtMenuName.Text = value;
            }
        }

        public string Picture
        {
            get
            {
                return picture;
            }
            set
            {
                byte[] imageBytes64 = Convert.FromBase64String(value);
                MemoryStream ms64 = new MemoryStream(imageBytes64, 0,
                  imageBytes64.Length);

                // Convert byte[] to Image
                ms64.Write(imageBytes64, 0, imageBytes64.Length);
                Image imagebase64 = Image.FromStream(ms64, true);
                Bitmap finalImg = new Bitmap(imagebase64, 115, 177);
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                pictureBox1.Image = finalImg;
                //picture = value;
            }
        }

        public int Menu_UnitID
        {
            get
            {
                return (int)cbUnit.EditValue;
            }
            set
            {
                cbUnit.EditValue = value;
            }
        }
        public Units Unit_Selected
        {
            get
            {
                return lsUnit.Find(delegate(Units id)
                {
                    return id.UnitID == (int)cbUnit.EditValue;
                });
            }
        }
        #endregion

        private void MenuProcess_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !dxValidationMenu.Validate() && DialogResult == DialogResult.OK;
        }

        private void btnMenuType_DoubleClick(object sender, EventArgs e)
        {
            //using (MenuTypeProcess frmMenuType = new MenuTypeProcess())
            //{
            //    if (frmMenuType.ShowDialog(this.FindForm()) == DialogResult.OK)
            //    {
            //        MenuType menuTypeObj = new MenuType();
            //        menuTypeObj.MenuTypeID = Guid.NewGuid();
            //        menuTypeObj.MenuTypeName = frmMenuType.MT_Name;
            //        menuTypeObj.Description = frmMenuType.MT_Des;
            //        iQuery.MT_NewMenuType(menuTypeObj);
            //        lsMenuType.Add(menuTypeObj);
            //        GetMenuTypes();
            //    }
            //}
        }

        private void btnUnit_DoubleClick(object sender, EventArgs e)
        {
            //using (UnitProcess unitProcess = new UnitProcess())
            //{
            //    if (unitProcess.ShowDialog(this.FindForm()) == DialogResult.OK)
            //    {
            //        Unit unitObj = new Unit();
            //        unitObj.UnitID = Guid.NewGuid();
            //        unitObj.UnitName = unitProcess.Unit_Name;
            //        unitObj.Description = unitProcess.Unit_Des;
            //        iQuery.Uni_NewUnit(unitObj);
            //        lsUnit.Add(unitObj);
            //        GetUnits();
            //    }
            //}
        }

        private void btnUnit_Click(object sender, EventArgs e)
        {
            using (UnitProcess unitProcess = new UnitProcess())
            {
                if (unitProcess.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    Units unitObj = new Units();
                    unitObj.UnitName = unitProcess.Unit_Name;
                    unitObj.Description = unitProcess.Unit_Des;
                    iQuery.InsertUnit(unitObj.UnitName,unitObj.Description);
                    lsUnit.Add(unitObj);
                    GetUnits();
                }
            }
        }

        private void btnMenuType_Click(object sender, EventArgs e)
        {
            using (MenuTypeProcess frmMenuType = new MenuTypeProcess())
            {
                if (frmMenuType.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    MenuTypes menuTypeObj = new MenuTypes();
                    menuTypeObj.MenuTypeName = frmMenuType.MT_Name;
                    menuTypeObj.Description = frmMenuType.MT_Des;
                    iQuery.InsertMenuType(menuTypeObj.MenuTypeName,menuTypeObj.Description);
                    lsMenuType.Add(menuTypeObj);
                    GetMenuTypes();
                }
            }
        }

       

        private void btnChooseImage_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofDlg = new OpenFileDialog();
            ofDlg.Filter = "JPG|*.jpg|GIF|*.gif|PNG|*.png|BMP|*.bmp";
            if (DialogResult.OK == ofDlg.ShowDialog())
            {
                string file = ofDlg.FileName;
                // QSecureEncoding Encoding = new QSecureEncoding();
                // PictureBox PictureBox1 = new PictureBox();
                //   PictureBox1.Image = new Bitmap(ofDlg.FileName);
                Image image = new Bitmap(ofDlg.FileName);
                string base64String = "";
                //System.Drawing.Imaging.ImageFormat format;
                using (MemoryStream ms = new MemoryStream())
                {

                    // Convert Image to byte[]
                    image.Save(ms, ImageFormat.Jpeg);
                    byte[] imageBytes = ms.ToArray();

                    // Convert byte[] to Base64 String
                    base64String = Convert.ToBase64String(imageBytes);
                    //Console.Write(base64String);
                    // return base64String;
                    picture = base64String;
                }

                // Convert Base64 String to byte[]
                byte[] imageBytes64 = Convert.FromBase64String(base64String);
                MemoryStream ms64 = new MemoryStream(imageBytes64, 0,
                  imageBytes64.Length);

                // Convert byte[] to Image
                ms64.Write(imageBytes64, 0, imageBytes64.Length);
                Image imagebase64 = Image.FromStream(ms64, true);
                Bitmap finalImg = new Bitmap(imagebase64, 115, 177);
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                pictureBox1.Image = finalImg;
            }
            else
            {
                MessageBox.Show("Go ahead, select a file!");
            }

        }
    }
}